package com.example.ec.controller;

import com.example.ec.model.Goods;
import com.example.ec.service.GoodsService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("goods")
public class GoodsController extends BaseController{

    private final GoodsService goodsService;

    public GoodsController(GoodsService goodsService) {
        this.goodsService = goodsService;
    }

    @GetMapping("create")
    private  String create(Goods goods){
        goodsService.create(goods);
        return "redirect:/goods/queryAll";

    }
    @GetMapping("queryAll")
    private String queryAll(){
        session.setAttribute("goodss",goodsService.queryAll());
        return "redirect:/admin/list.jsp";
    }

    @GetMapping("removeById/{id}")
        private String removeById(@PathVariable("id") int id){

        goodsService.removeById(id);
        session.setAttribute("goodss",goodsService.queryAll());
        return "redirect:/admin/list.jsp";
    }

    @GetMapping("queryById/{id}")
    private String queryById(@PathVariable("id") int id){
       session.setAttribute("good",goodsService.queryById(id));
        return "redirect:/admin/edit.jsp";

    }
    @PostMapping("modify")
    private String modify(Goods goods){
        goodsService.modify(goods);
        return "redirect:/goods/queryAll";


    }

    @GetMapping("queryGoods/{id}")
    private String queryGoods(@PathVariable("id") int id){
        session.setAttribute("good", goodsService.queryById(id));
        return "redirect:/admin/details.jsp";
    }
    @PostMapping("queryByBrand")
    private String queryByBrand(Goods goods){
         if(goodsService.queryOne("queryByBrand",goods)==null){
             session.setAttribute("error1","no goods");
             return "redirect:/user/index.jsp";
         }
         session.setAttribute("goods1",goodsService.queryList("queryByBrand",goods));
         return "redirect:/user/index.jsp";
    }
    @GetMapping("queryBrandSearch/{brand}")
    private String queryBrandSearch(@PathVariable("brand") String brand) {
        session.setAttribute("good1",goodsService.queryList("queryBrandSearch",brand));
        return "redirect:/user/details.jsp";
    }

}
